#!/usr/bin/python3
# Version = 3.6.8
# -*- coding:utf-8 -*-

import os
import json
import datetime
import write_version
import sys


#svn checkout "svn_path" --revision "svn_version" code_dir
#构建目录默认是"source_dir"的新建build目录
#cmake .. -D"build_option"=on -DCMAKE_BUILD_TYPE="build_type" -G"Unix Makefiles"
#make 2>>compile_error.txt

#更新Version.h中的版本信息，这个是对内容格式有要求

class Target:
    def __init__(self) -> None:
        self.target_dir = ""  #目标模块的目录
        self.build_option = ""
        self.build_type = ""
        self.build_version = ""

    def __str__(self) -> str:
        pass

g_root_dir = ""

#build_option, output_direcotry
g_output_dir_dict = {
    "BUILD_HST2":"run/HST2",
    "BUILD_HST3": "run/HST3"
}

# config.json
def ParseConfig():
    confFile = open("./config.json", "r")
    jsonCont = json.load(confFile)

    svn_path = jsonCont["svn_path"]
    svn_version = jsonCont["svn_version"]

    target = Target()
    target.target_dir = jsonCont["target"]["target_dir"]
    target.build_option = jsonCont["target"]["build_option"]
    target.build_type = jsonCont["target"]["build_type"]
    target.build_version = jsonCont["target"]["build_version"]

    return svn_path, svn_version, target


def CheckOutCode(svn_path, svn_version):
    now = datetime.datetime.now()
    source_code_dir = "%04d%02d%02d_%02d%02d" % (now.year, now.month, now.day, now.hour, now.minute)

    if not os.path.exists(source_code_dir):
        os.mkdir(source_code_dir)

    co_cmd = "svn checkout {} {}".format(svn_path, source_code_dir)
    if svn_version != "":
        co_cmd = "svn checkout {} --revision {} {}".format(svn_path, svn_version, source_code_dir)
    
    os.system(co_cmd)
    os.chdir(source_code_dir)
    print("source directory: ", os.getcwd())

    os.mkdir("Result")

    os.system("svn info {} > Result/svn_info.txt".format(svn_path))
    os.system("echo {} >> Result/svn_info.txt".format(co_cmd))


def ExecUnitTest():
    os.chdir("UnitTest")
    script_name = "run_test.py"
    file_names = os.listdir("./")
    for item in file_names:
        if item == script_name:
            os.system("chmod +x {}".format(script_name))
            os.system("./{}".format(script_name))
            break
    os.chdir("../")

def CompileCode(target:Target):
    #pb-trade-server dir
    print("-----------------------------\nCompileCode path: ", os.getcwd())
    os.chdir(target.target_dir)
    write_version.WriteVersionToFile('Version.h', target.build_version)

    os.mkdir("build")
    os.chdir("build")
    print("Compile directory: ", os.getcwd())
    cmake_cmd = "cmake .. -D{}=on -DCMAKE_BUILD_TYPE={} -G\"Unix Makefiles\"".format(target.build_option, target.build_type)
    os.system(cmake_cmd)
    #os.system("make 2>>compile_error.txt")
    #os.system("make -j 3 2>>compile_error.txt")
    os.system("make -j 3")
    
    os.chdir("../")
    cp_cmd ="cp {}/* ../Result/".format(g_output_dir_dict[target.build_option])
    print("copy command :", cp_cmd)
    os.system(cp_cmd)


if __name__ == '__main__':
    g_root_dir = os.getcwd()
    print("root directory:", g_root_dir)

    svn_path, svn_version, target = ParseConfig()

    CheckOutCode(svn_path, svn_version)

    ExecUnitTest()

    CompileCode(target)

